﻿'********************************************************************************
'*
'*      エラーMAP
'*
'*      File Name : frmErrorMap.vb
'*      Title     : エラーMAP
'*      機能    　：フレーム毎にエラーコード入力
'*
'********************************************************************************
'----------------------------------<<修正履歴>>----------------------------------
'ﾊﾞｰｼﾞｮﾝ   日付       作業者        ｺﾒﾝﾄ
'--------- ---------- ------------ -----------------------------------------------
' V1.0.0   2016/08/30 S.Yu         新規作成
'********************************************************************************

Imports System.Text

Public Class frmErrorMap

    Private ReadOnly COLOR_WHITE As Color = Drawing.SystemColors.ButtonHighlight
    Private ReadOnly COLOR_CONTROL As Color = Drawing.SystemColors.Control
    Private ReadOnly COLOR_L_YELLOW As Color = Color.LightYellow
    Private ReadOnly COLOR_L_GREEN As Color = Color.LightGreen
    Private ReadOnly COLOR_RED As Color = Color.Red
    Private ReadOnly COLOR_GREEN As Color = Color.Lime
    Private ReadOnly COLOR_YELLOW As Color = Color.Yellow
    Private ReadOnly COLOR_NAVY As Color = Color.DodgerBlue
    Private ReadOnly COLOR_CYAN As Color = Color.Cyan

    Private Enum TenkeyType
        Qty
        Row
        Col
    End Enum

    Private ReadOnly ROW_LABEL() As String = {" ", "A", "B", "C", "D", "E", "F", "G", "H", _
                                              "I", "J", "K", "L", "M", "N", "O", "P", "Q", _
                                              "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " "}

    Private m_dtRackFrameInfo As DataTable
    Private m_dtDefectMapInfo As DataTable
    'Private m_iRackNo As Integer
    Private m_szEquipID As String
    Private m_fIsBorderClicked As Boolean = False
    Public htMsgMst As New Hashtable

    Public WriteOnly Property EquipID As String
        Set(ByVal value As String)
            m_szEquipID = value
        End Set
    End Property

    ''' <summary>
    ''' 画面ロード
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub frmErrorMap_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitMagzine()
        InitTenKey()
        GetDefectMapInfo()
        GetRackFrameInfo()
        GetDefectCata()
    End Sub

    ''' <summary>
    ''' 「PQC」提出ボタンの有効化
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub panelFrmLst_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles panelFrmLst.EnabledChanged
        btnPQC.Enabled = panelFrmLst.Enabled
    End Sub

    ''' <summary>
    ''' 画面空白部分クリック
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Space_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles Me.MouseClick, panelMagazine.MouseClick, panelFrmLst.MouseClick, panelRack.MouseClick, panelFrame.MouseClick

        btnPQC.BackColor = COLOR_CONTROL
        btnPQC.UseVisualStyleBackColor = True
        btnPQC.Tag = False

        If IsNothing(btnFrame) OrElse IsNothing(btnFrame.Tag) Then
            Exit Sub
        End If
        If Not btnFrame.Tag.Equals(NG) Then
            ResetFrame(False)
            SetFrameInRack()
        End If
    End Sub

    ''' <summary>
    ''' 「保存」ボタンクリック
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim sbMsg As New StringBuilder
        If cLotDefect.SaveRackFrameInfo(m_dtRackFrameInfo, lblLotNo.Text) < 0 Then
            sbMsg.AppendLine(htMsgMst("MSG00082"))
        End If
        If cLotDefect.SaveDefectMapInfo(m_dtDefectMapInfo, lblLotNo.Text, m_szEquipID, g_EQUIP_NO) < 0 Then
            sbMsg.AppendLine(htMsgMst("MSG00083"))
        End If
        If String.IsNullOrEmpty(sbMsg.ToString) Then
            MsgBox(htMsgMst("MSG00084"))
        Else
            MsgBox(sbMsg.ToString, MsgBoxStyle.Critical)
        End If
    End Sub

    ''' <summary>
    ''' 「PQC提出」ボタンクリック
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnPQC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPQC.Click
        'Dim dr() As DataRow = m_dtRackFrameInfo.Select("RACK_NO=" & lblRackNo.Text)
        'If dr.Length > 0 Then
        '    If Not CBool(dr(0)("PQC_SUBMIT")) Then
        '        UpdateRackFrameInfo(True, "PQC_SUBMIT", cint(lblRackNo.Text))
        '        'dr(0)("PQC_SUBMIT") = True
        '        SetFrameInRack()
        '        ResetFrame(True)
        '        btnPQC.BackColor = COLOR_GREEN
        '    End If
        'End If
        If CBool(btnPQC.Tag) Then
            btnPQC.BackColor = COLOR_CONTROL
            btnPQC.UseVisualStyleBackColor = True
            btnPQC.Tag = False
        Else
            btnPQC.BackColor = COLOR_GREEN
            btnPQC.Tag = True
        End If
    End Sub

#Region "マガジン"
    Private Const RACK_CNT As Integer = 10
    Dim btnRackArr(RACK_CNT - 1) As Button
    Dim btnLastRack As New Button
    Private Sub InitMagzine()
        Dim i As Integer
        For i = 0 To RACK_CNT - 1
            btnRackArr(i) = panelMagazine.Controls.Find("btnRack" & (i + 1).ToString, False)(0)
            AddHandler btnRackArr(i).Click, AddressOf btnRack_Click
        Next
    End Sub

    ''' <summary>
    ''' ラック選択クリック
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnRack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btnRack As Button = CType(sender, Button)
        lblRackNo.Text = btnRack.Text
        'ClearRack()
        ResetFrame(False)
        panelRack.Enabled = True
        txtRackID.Text = btnRack.Tag
        lblRackID.Text = btnRack.Tag
        'Dim dr() As DataRow = m_dtRackFrameInfo.Select("RACK_NO=" & lblRackNo.Text)
        If m_dtRackFrameInfo.Rows.Count > 0 Then
            Dim curRows() As DataRow = m_dtRackFrameInfo.Select("", "", DataViewRowState.CurrentRows)
            lblFrameQty.Text = curRows(0)("FRAME_CNT").ToString
        Else
            lblFrameQty.Text = "0"
        End If

        'If txtRackID.TextLength > 0 Then
        panelFrmLst.Enabled = True
        'Else
        '    panelFrmLst.Enabled = False
        'End If

        If Not String.IsNullOrEmpty(btnLastRack.Text) Then
            Dim drFrameDefect() As DataRow = m_dtDefectMapInfo.Select( _
                        String.Format("RACK_NO={0}", btnLastRack.Text))
            If drFrameDefect.Length > 0 Then
                If IsPqcSubmitted(btnLastRack.Text) Then
                    btnLastRack.BackColor = COLOR_GREEN
                Else
                    btnLastRack.BackColor = COLOR_RED
                End If
            Else
                btnLastRack.BackColor = COLOR_L_YELLOW
            End If
        End If

        'If IsPqcSubmitted(btnRack.Text) Then
        '    btnPQC.BackColor = COLOR_GREEN
        'Else
        'btnPQC.BackColor = COLOR_CONTROL
        'btnPQC.UseVisualStyleBackColor = True
        'btnPQC.Tag = False
        'End If

        btnRack.BackColor = COLOR_NAVY

        SetFrameInRack()
        btnFrame = New Button
        btnLastRack = btnRack
    End Sub
#End Region

#Region "ラック"
    Private iFrmQty As Integer
    Private btnFrame As Button
    Private btnLastFrame As New Button
    Dim btnFrameArr() As Button

    Private Sub SetFrameInRack()
        iFrmQty = CInt(lblFrameQty.Text)
        panelFrmLst.Controls.Clear()

        'btnPQC.BackColor = COLOR_CONTROL
        'btnPQC.UseVisualStyleBackColor = True
        'btnPQC.Tag = False

        ReDim btnFrameArr(iFrmQty - 1)
        Dim iSpaceX, iSpaceY As Integer
        Dim i As Integer

        Dim iRowCnt As Integer = 4
        Dim iColCnt As Integer = 10
        iSpaceX = Int(panelFrmLst.Width / iColCnt)
        iSpaceY = Int(panelFrmLst.Height / iRowCnt)
        For i = 0 To iFrmQty - 1
            btnFrameArr(i) = New Button
            With btnFrameArr(i)
                .Name = "btnFrame" & (i + 1).ToString
                .Text = (i + 1).ToString
                .Width = iSpaceX - 8
                .Height = iSpaceY - 8
                .Left = 4 + iSpaceX * (i Mod iColCnt)
                .Top = 4 + iSpaceY * Int(i / iColCnt)

                Dim iFrameIdx As Integer
                If CBool(btnRackTrans.Tag) Then
                    iFrameIdx = iFrmQty - CInt(.Text) + 1
                Else
                    iFrameIdx = CInt(.Text)
                End If

                Dim drFrameDefect() As DataRow = m_dtDefectMapInfo.Select( _
                    String.Format("RACK_NO={0} AND DANSU={1}", lblRackNo.Text, iFrameIdx))
                If drFrameDefect.Length > 0 Then
                    .Tag = NG
                    If IsPqcSubmitted(, iFrameIdx) Then
                        .BackColor = COLOR_GREEN
                    Else
                        .BackColor = COLOR_RED
                    End If
                Else
                    .Tag = String.Empty
                End If
            End With
            AddHandler btnFrameArr(i).Click, AddressOf btnFrame_Click
            panelFrmLst.Controls.Add(btnFrameArr(i))
        Next

        lblTotalDefect.Text = DefectCount.ToString
        lblDefectInRack.Text = DefectCountInRack.ToString
    End Sub

    Private Sub btnFrame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        btnFrame = CType(sender, Button)
        panelFrame.Enabled = True

        Dim iFrameIdx As Integer
        If Not String.IsNullOrEmpty(btnLastFrame.Text) Then
            If CBool(btnRackTrans.Tag) Then
                iFrameIdx = iFrmQty - CInt(btnLastFrame.Text) + 1
            Else
                iFrameIdx = CInt(btnLastFrame.Text)
            End If
        End If

        If Not String.IsNullOrEmpty(btnLastFrame.Tag) AndAlso btnLastFrame.Tag.Equals(NG) Then
            If IsPqcSubmitted(, iFrameIdx) Then
                btnLastFrame.BackColor = COLOR_GREEN
            Else
                btnLastFrame.BackColor = COLOR_RED
            End If
        Else
            btnLastFrame.BackColor = COLOR_CONTROL
            btnLastFrame.UseVisualStyleBackColor = True
            'btnLastFrame.Tag = False
        End If

        btnFrame.BackColor = COLOR_NAVY
        'btnFrame.Tag = True
        btnLastFrame = btnFrame

        ResetFrame(True)
    End Sub

    ''' <summary>
    ''' ラック「反転」クリック
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnRackTrans_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRackTrans.Click
        Dim i As Integer
        Dim fIsSelected As Boolean = False
        If Not IsNothing(btnFrame) AndAlso btnFrame.BackColor = COLOR_NAVY Then
            fIsSelected = True
        End If
        If (CBool(btnRackTrans.Tag)) Then
            For i = 0 To iFrmQty - 1
                Dim drFrameDefect() As DataRow = m_dtDefectMapInfo.Select( _
                        String.Format("RACK_NO={0} AND DANSU={1}", lblRackNo.Text, i + 1))
                With btnFrameArr(i)
                    If drFrameDefect.Length > 0 Then
                        If IsPqcSubmitted() Then
                            .BackColor = COLOR_GREEN
                        Else
                            .BackColor = COLOR_RED
                        End If
                        .Tag = NG
                    Else
                        .BackColor = COLOR_CONTROL
                        .UseVisualStyleBackColor = True
                        .Tag = String.Empty
                    End If
                End With
            Next
            btnRackTrans.Tag = False
            btnRackTrans.BackColor = COLOR_CONTROL
            btnRackTrans.UseVisualStyleBackColor = True
        Else
            For i = 0 To iFrmQty - 1
                Dim drFrameDefect() As DataRow = m_dtDefectMapInfo.Select( _
                        String.Format("RACK_NO={0} AND DANSU={1}", lblRackNo.Text, iFrmQty - i))
                With btnFrameArr(i)
                    If drFrameDefect.Length > 0 Then
                        If IsPqcSubmitted() Then
                            .BackColor = COLOR_GREEN
                        Else
                            .BackColor = COLOR_RED
                        End If
                        .Tag = NG
                    Else
                        .BackColor = COLOR_CONTROL
                        .UseVisualStyleBackColor = True
                        .Tag = String.Empty
                    End If
                End With
            Next
            btnRackTrans.Tag = True
            btnRackTrans.BackColor = COLOR_YELLOW
        End If

        If fIsSelected Then
            Dim idx As Integer = CInt(btnFrame.Text)
            btnFrame = btnFrameArr(iFrmQty - idx)
            btnFrame.BackColor = COLOR_NAVY
        End If

        UpdateRackFrameInfo(CBool(btnRackTrans.Tag), "RACK_TANSF")
    End Sub

    Private Sub btnRackID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRackID.Click
        lblRackID.BackColor = COLOR_CYAN
        txtRackID.BackColor = COLOR_CYAN
        txtRackID.Enabled = True
        txtRackID.Focus()
        txtRackID.SelectAll()
    End Sub

    Private Sub txtRackID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRackID.KeyPress
        '頭に付いている'%'を消す
        Static Dim fIsPercentInput As Boolean = False
        If e.KeyChar = CChar("%") OrElse e.KeyChar = CChar("％") Then
            fIsPercentInput = True
            e.Handled = True
            Exit Sub
        End If
        If fIsPercentInput Then
            fIsPercentInput = False
            e.Handled = True
            Exit Sub
        End If

        If e.KeyChar = vbCr Then
            Dim txtData As TextBox = CType(sender, TextBox)
            txtData.Enabled = False
        End If
    End Sub

    Private Const RACK_BARCODE_LENGTH As Integer = 5
    Private Sub txtRackID_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRackID.LostFocus
        If txtRackID.Text.Length <= 0 Then
            btnRackArr(CInt(lblRackNo.Text) - 1).Tag = String.Empty
            lblRackID.Text = txtRackID.Text

        Else
            Dim szRackArr() As String = txtRackID.Text.Split(CChar(";"))
            If szRackArr.Length < RACK_BARCODE_LENGTH Then
                txtRackID.Text = btnRackArr(CInt(lblRackNo.Text) - 1).Tag
                lblRackID.Text = txtRackID.Text
            Else
                Dim szRackID As String = szRackArr(2).Substring(3, 4)
                Dim drRack() As DataRow = m_dtRackFrameInfo.Select(String.Format("RACK_ID='{0}'", szRackID))
                'If drRack.Length > 0 Then
                '    MsgBox(String.Format("ID「{0}」のラックは重複しています。", szRackID))
                '    txtRackID.Text = btnRackArr(cint(lblRackNo.Text) - 1).Tag
                '    lblRackID.Text = txtRackID.Text
                'Else
                txtRackID.Text = szRackID
                lblRackID.Text = szRackID
                Dim dr() As DataRow = m_dtRackFrameInfo.Select("RACK_NO=" & lblRackNo.Text)
                If dr.Length > 0 Then
                    dr(0)("RACK_ID") = txtRackID.Text
                Else
                    AddNewRack()
                End If
                btnRackArr(CInt(lblRackNo.Text) - 1).Tag = txtRackID.Text
                'End If
            End If
        End If
        txtRackID.BackColor = COLOR_WHITE
        lblRackID.BackColor = COLOR_WHITE

        'If txtRackID.TextLength > 0 Then
        panelFrmLst.Enabled = True
        'Else
        '    panelFrmLst.Enabled = False
        'End If

        UpdateRackFrameInfo(txtRackID.Text, "RACK_ID", CInt(lblRackNo.Text))
    End Sub

    Private Sub AddNewRack()
        Dim newRow As DataRow = m_dtRackFrameInfo.NewRow
        newRow("LOT_NO") = lblLotNo.Text
        newRow("RACK_NO") = CInt(lblRackNo.Text)
        newRow("RACK_ID") = txtRackID.Text
        newRow("RACK_TANSF") = CBool(btnRackTrans.Tag)
        newRow("FRAME_CNT") = CInt(lblFrameQty.Text)
        newRow("FRM_COL_CNT") = CInt(lblFrameRow.Text)
        newRow("FRM_ROW_CNT") = CInt(lblFrameCol.Text)
        newRow("FRM_TANSF") = CBool(btnRackTrans.Tag)
        newRow("PQC_SUBMIT") = CBool(btnPQC.Tag)
        m_dtRackFrameInfo.Rows.Add(newRow)
    End Sub
#End Region

#Region "フレーム"
    Private Sub FrameChanged()
        Dim iRow As Integer = CInt(lblFrameRow.Text)
        Dim iCol As Integer = CInt(lblFrameCol.Text)

        dgvFrame.Columns.Clear()
        If iRow = 0 OrElse iCol = 0 Then
            Exit Sub
        End If

        Dim i, j As Integer
        For i = 0 To iRow + 1
            dgvFrame.Columns.Add(i.ToString, i.ToString)
        Next

        For i = 0 To iCol + 1
            Dim szRow(iRow + 1) As String
            szRow(0) = ROW_LABEL(i)
            szRow(iRow + 1) = ROW_LABEL(i)
            For j = 1 To iRow
                If i = 0 Or i = iCol + 1 Then
                    szRow(0) = " "
                    szRow(iRow + 1) = " "
                    szRow(j) = iRow + 1 - j
                Else
                    szRow(j) = String.Format("{0}-{1}", ROW_LABEL(i), iRow + 1 - j)
                End If
            Next

            dgvFrame.Rows.Add(szRow)
        Next

        SetFrameFormat()
    End Sub

    Private Const FRM_TITLE_CELL_WIDTH As Integer = 28
    Private Const FRM_TITLE_CELL_HEIGHT As Integer = 22
    Private Sub SetFrameFormat()
        With dgvFrame
            Dim iRow As Integer = CInt(lblFrameRow.Text)
            Dim iCol As Integer = CInt(lblFrameCol.Text)
            Dim iFrmWidth = .Width
            Dim iFrmHeight = .Height

            Dim iHeight As Integer = Int((.Height - FRM_TITLE_CELL_HEIGHT * 2) / iCol)
            Dim iWidth As Integer = Int((.Width - FRM_TITLE_CELL_WIDTH * 2) / iRow)

            Dim i As Integer
            For i = 1 To iRow
                '.Columns(i).DefaultCellStyle.BackColor = COLOR_WHITE
                .Columns(i).Width = iWidth
            Next
            For i = 1 To iCol
                '.Rows(i).DefaultCellStyle.BackColor = COLOR_WHITE
                .Rows(i).Height = iHeight
            Next

            .Columns(0).DefaultCellStyle.BackColor = COLOR_L_GREEN
            .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(0).Width = Int((.Width - iWidth * iRow) / 2) - 1
            .Columns(iRow + 1).DefaultCellStyle.BackColor = COLOR_L_GREEN
            .Columns(iRow + 1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(iRow + 1).Width = Int((.Width - iWidth * iRow) / 2) - 1
            .Rows(0).DefaultCellStyle.BackColor = COLOR_L_GREEN
            .Rows(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Rows(0).Height = Int((.Height - iHeight * iCol) / 2) - 1
            .Rows(iCol + 1).DefaultCellStyle.BackColor = COLOR_L_GREEN
            .Rows(iCol + 1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Rows(iCol + 1).Height = Int((.Height - iHeight * iCol) / 2) - 1

            .ClearSelection()
        End With
    End Sub

    ''' <summary>
    ''' フレーム表示をリセットする
    ''' </summary>
    ''' <param name="fEnable">リセット後の状態</param>
    ''' <remarks></remarks>
    Private Sub ResetFrame(ByVal fEnable As Boolean)
        dgvFrame.Columns.Clear()
        FrameChanged()
        panelFrame.Enabled = fEnable
        If Not fEnable Then
            btnFrame = New Button
        End If

        'btnPQC.BackColor = COLOR_CONTROL
        'btnPQC.UseVisualStyleBackColor = True
        'btnPQC.Tag = False
    End Sub

    Private Const CODE_SPLIT As String = "::"
    Private Sub dgvFrame_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvFrame.CellMouseUp
        If m_fIsBorderClicked Then
            dgvFrame.ClearSelection()
            Exit Sub
        End If

        Dim iRow As Integer = CInt(lblFrameRow.Text)
        Dim iCol As Integer = CInt(lblFrameCol.Text)

        If e.RowIndex = 0 Or e.RowIndex = iCol + 1 Or _
           e.ColumnIndex = 0 Or e.ColumnIndex = iRow + 1 Then
            dgvFrame.ClearSelection()
            Exit Sub
        End If


        Dim szCode As String = String.Empty
        Dim szName As String = String.Empty
        If dgvFrame.SelectedCells.Count = 1 Then
            Dim cell As DataGridViewCell = dgvFrame.Item(e.ColumnIndex, e.RowIndex)
            If Not IsNothing(cell.Tag) Then
                Dim szTmp = cell.Tag.ToString.Replace(vbCr, "").Split(CODE_SPLIT)
                If szTmp.Length >= 2 Then
                    szCode = szTmp(0)
                    szName = szTmp(szTmp.Length - 1)
                End If
            End If
        End If

        Dim dfSelect As frmDefectSelect.Defect = SelectCata(szCode, szName)
        If Not (IsNothing(dfSelect.szCode) Or IsNothing(dfSelect.szName)) AndAlso _
            (dfSelect.szCode.Equals(OK) And dfSelect.szName.Equals(OK)) Then
            '良品
            For Each cell As DataGridViewCell In dgvFrame.SelectedCells
                Dim iRowIdx As Integer = cell.RowIndex
                Dim iColIdx As Integer = cell.ColumnIndex
                DeleteDefectInfo(iColIdx, iRowIdx)
            Next
            'ラックボタンの再表示
            Dim lblRack As Button = btnRackArr(CInt(lblRackNo.Text) - 1)
            If Not String.IsNullOrEmpty(lblRack.Text) Then
                Dim drFrameDefect() As DataRow = m_dtDefectMapInfo.Select( _
                            String.Format("RACK_NO={0}", lblRack.Text))
                If drFrameDefect.Length > 0 Then
                    If IsPqcSubmitted(lblRack.Text) Then
                        lblRack.BackColor = COLOR_GREEN
                    Else
                        lblRack.BackColor = COLOR_RED
                    End If
                Else
                    lblRack.BackColor = COLOR_L_YELLOW
                    btnFrame.Tag = String.Empty
                    'btnFrame.UseVisualStyleBackColor = True
                End If
            End If
        Else
            For Each cell As DataGridViewCell In dgvFrame.SelectedCells
                Dim iRowIdx As Integer = cell.RowIndex
                Dim iColIdx As Integer = cell.ColumnIndex
                Dim szTxtBase = dgvFrame.Item(0, iRowIdx).Value.ToString & "-" & dgvFrame.Item(iColIdx, 0).Value.ToString
                If Not String.IsNullOrEmpty(dfSelect.szCode) Then
                    cell.Value = String.Format("{0}" & vbCrLf & "{1}", szTxtBase, dfSelect.szName)
                    cell.Tag = String.Format("{0}" & CODE_SPLIT & "{1}", dfSelect.szCode, dfSelect.szName).Replace(vbCr, "")
                    btnFrame.Tag = NG

                    UpdateDefectMapInfo(iColIdx, iRowIdx)
                End If
            Next

            If CBool(btnPQC.Tag) Then
                For Each cell As DataGridViewCell In dgvFrame.SelectedCells
                    Dim iRowIdx As Integer = cell.RowIndex
                    Dim iColIdx As Integer = cell.ColumnIndex
                    If Not String.IsNullOrEmpty(cell.Tag) Then
                        'cell.Style.BackColor = COLOR_GREEN
                        UpdateRackFrameInfo(True, "PQC_SUBMIT", CInt(lblRackNo.Text))
                        UpdateDefectMapInfo(iColIdx, iRowIdx, True)
                    End If
                Next
            End If
        End If

        '「PQC提出」ボタンをリセット
        btnPQC.BackColor = COLOR_CONTROL
        btnPQC.UseVisualStyleBackColor = True
        btnPQC.Tag = False

        ResetFrame(True)
    End Sub

    Private Sub dgvFrame_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvFrame.CellMouseDown
        Dim iRow As Integer = CInt(lblFrameRow.Text)
        Dim iCol As Integer = CInt(lblFrameCol.Text)

        If e.RowIndex = 0 Or e.RowIndex = iCol + 1 Or _
           e.ColumnIndex = 0 Or e.ColumnIndex = iRow + 1 Then
            dgvFrame.ClearSelection()
            m_fIsBorderClicked = True
        Else
            m_fIsBorderClicked = False
        End If
    End Sub

    ''' <summary>
    ''' フレーム図面表示する時に不良カタゴリを適用する
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgvFrame_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvFrame.CellPainting
        If IsNothing(btnFrame) OrElse btnFrame.BackColor <> COLOR_NAVY Then
            Exit Sub
        End If

        Dim iFrameIdx As Integer
        If CBool(btnRackTrans.Tag) Then
            iFrameIdx = iFrmQty - CInt(btnFrame.Text) + 1
        Else
            iFrameIdx = CInt(btnFrame.Text)
        End If

        Dim iRow As Integer = e.RowIndex
        Dim iCol As Integer = e.ColumnIndex
        If CBool(btnFrameTrans.Tag) Then
            iCol = CInt(lblFrameRow.Text) - iCol + 1
            iRow = CInt(lblFrameCol.Text) - iRow + 1
        End If

        If dgvFrame.Enabled Then
            Dim drFrameDefect() As DataRow = m_dtDefectMapInfo.Select( _
                                String.Format("RACK_NO={0} AND DANSU={1} AND MAP_X='{2}' AND MAP_Y='{3}'", _
                                              lblRackNo.Text, iFrameIdx, iCol, iRow))
            If drFrameDefect.Length > 0 Then
                With dgvFrame
                    Dim szTxtBase = .Item(e.ColumnIndex, 0).Value.ToString & "-" & .Item(0, e.RowIndex).Value.ToString
                    Dim cell As DataGridViewCell = .Item(e.ColumnIndex, e.RowIndex)
                    cell.Value = String.Format("{0}" & vbCrLf & "{1}", szTxtBase, drFrameDefect(0)("CAT_NAME").ToString)
                    cell.Tag = String.Format("{0}" & CODE_SPLIT & "{1}", drFrameDefect(0)("CAT_CD"), drFrameDefect(0)("CAT_NAME")).Replace(vbCr, "")
                    If IsPqcSubmitted(, iFrameIdx, iCol, iRow) Then
                        cell.Style.BackColor = COLOR_GREEN
                    Else
                        cell.Style.BackColor = COLOR_RED
                    End If
                End With
            End If
        End If

    End Sub

    ''' <summary>
    ''' フレーム「反転」クリック
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnFrameTrans_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFrameTrans.Click
        With btnFrameTrans
            If CBool(.Tag) Then
                .Tag = False
                .BackColor = COLOR_CONTROL
                .UseVisualStyleBackColor = True
            Else
                .Tag = True
                .BackColor = COLOR_YELLOW
            End If

            ResetFrame(True)

            UpdateRackFrameInfo(.Tag, "FRM_TANSF")
        End With
    End Sub

#End Region

#Region "不良カタゴリ"

    Private cLotDefect As New clsDbLotDefect
    Private dtDefectCata As DataTable
    Private Sub GetDefectCata()
        Dim dt As DataTable = cLotDefect.GetDefectCategory(g_CURRENT_EQUIP_TYPE)
        If Not IsNothing(dt) AndAlso dt.Columns.Count >= 2 Then
            dtDefectCata = dt
        Else
            dtDefectCata = New DataTable
            MsgBox(htMsgMst("MSG00085"))
        End If
    End Sub

    Private Function SelectCata(ByVal szCode As String, ByVal szName As String) As frmDefectSelect.Defect
        Dim frmSelect As New frmDefectSelect
        frmSelect.htMsgMst = htMsgMst
        frmSelect.DefectCata = dtDefectCata
        frmSelect.StartPosition = FormStartPosition.CenterParent
        Dim df As frmDefectSelect.Defect
        df.szCode = szCode
        df.szName = szName
        frmSelect.SelectedCata = df
        Select Case frmSelect.ShowDialog()
            Case Windows.Forms.DialogResult.OK
                Return frmDefectSelect.SelectedCata
            Case Windows.Forms.DialogResult.Ignore
                Dim dfOK As frmDefectSelect.Defect
                dfOK.szCode = OK
                dfOK.szName = OK
                Return dfOK
            Case Else
                Return New frmDefectSelect.Defect
        End Select
    End Function

    ''' <summary>
    ''' PQC提出かどうかの判断
    ''' </summary>
    ''' <param name="szLastRack"></param>
    ''' <param name="iMapX"></param>
    ''' <param name="iMapY"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function IsPqcSubmitted(Optional ByVal szLastRack As String = "", _
                                    Optional ByVal iFrame As Integer = -1, _
                                    Optional ByVal iMapX As Integer = -1, Optional ByVal iMapY As Integer = -1) As Boolean
        Dim drDefect() As DataRow
        Dim szCond As String
        If String.IsNullOrEmpty(szLastRack) Then
            szCond = "RACK_NO=" & lblRackNo.Text
        Else
            szCond = "RACK_NO=" & szLastRack
        End If

        If iFrame > 0 Then
            szCond &= String.Format(" AND DANSU={0}", iFrame)
        End If

        If iMapX > 0 And iMapY > 0 Then
            szCond &= String.Format(" AND MAP_X={0} AND MAP_Y={1}", iMapX, iMapY)
        End If

        'szCond &= (" AND PQC_SUBMIT=TRUE")

        IsPqcSubmitted = False
        drDefect = m_dtDefectMapInfo.Select(szCond)
        For Each dr As DataRow In drDefect

            If Not IsDBNull(dr("PQC_SUBMIT")) AndAlso CBool(dr("PQC_SUBMIT")) Then
                IsPqcSubmitted = True
                Exit Function
            End If
        Next
    End Function

#End Region

#Region "データ"
    ''' <summary>
    ''' ラックとフレームの情報を取得する
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetRackFrameInfo()
        m_dtRackFrameInfo = cLotDefect.GetRackFrameInfo(lblLotNo.Text)
        If m_dtRackFrameInfo.Rows.Count <= 0 Then
            If m_dtRackFrameInfo.Columns.Count <= 0 Then
                m_dtRackFrameInfo.Columns.Add("LOT_NO", "".GetType)
                m_dtRackFrameInfo.Columns.Add("RACK_NO", 0.GetType)
                m_dtRackFrameInfo.Columns.Add("RACK_ID", "".GetType)
                m_dtRackFrameInfo.Columns.Add("RACK_TANSF", True.GetType)
                m_dtRackFrameInfo.Columns.Add("FRAME_CNT", 0.GetType)
                m_dtRackFrameInfo.Columns.Add("FRM_COL_CNT", 0.GetType)
                m_dtRackFrameInfo.Columns.Add("FRM_ROW_CNT", 0.GetType)
                m_dtRackFrameInfo.Columns.Add("FRM_TANSF", True.GetType)
                m_dtRackFrameInfo.Columns.Add("PQC_SUBMIT", True.GetType)
            End If
        Else
            'For Each dr As DataRow In m_dtRackFrameInfo.Rows
            '    Dim idx As Integer
            '    If Not IsDBNull(dr("RACK_NO")) Then
            '        idx = cint(dr("RACK_NO")) - 1
            '        btnRackArr(idx).Tag = dr("RACK_ID").ToString
            '        If CBool(dr("PQC_SUBMIT")) Then
            '            btnRackArr(idx).BackColor = COLOR_GREEN
            '        Else
            '            btnRackArr(idx).BackColor = COLOR_RED
            '        End If
            '    End If
            'Next

            For Each btn As Button In btnRackArr
                Dim drRf() As DataRow = m_dtRackFrameInfo.Select("RACK_NO=" & btn.Text)
                If drRf.Length > 0 Then
                    Dim drDm() As DataRow = m_dtDefectMapInfo.Select("RACK_NO=" & btn.Text)
                    If drDm.Length > 0 Then
                        If IsPqcSubmitted(btn.Text) Then
                            btn.BackColor = COLOR_GREEN
                        Else
                            btn.BackColor = COLOR_RED
                        End If
                        'End If
                    End If
                    btn.Tag = drRf(0)("RACK_ID").ToString

                End If
            Next

            If CBool(m_dtRackFrameInfo.Rows(0)("RACK_TANSF")) Then
                btnRackTrans.BackColor = COLOR_YELLOW
                btnRackTrans.Tag = True
            End If
            If CBool(m_dtRackFrameInfo.Rows(0)("FRM_TANSF")) Then
                btnFrameTrans.BackColor = COLOR_YELLOW
                btnFrameTrans.Tag = True
            End If

            lblFrameRow.Text = m_dtRackFrameInfo.Rows(0)("FRM_COL_CNT").ToString
            lblFrameCol.Text = m_dtRackFrameInfo.Rows(0)("FRM_ROW_CNT").ToString
            FrameChanged()
        End If
    End Sub

    Private Sub GetDefectMapInfo()
        m_dtDefectMapInfo = cLotDefect.GetDefectMapInfo(lblLotNo.Text, m_szEquipID)
        If m_dtDefectMapInfo.Rows.Count <= 0 Then
            If m_dtDefectMapInfo.Columns.Count <= 0 Then
                m_dtDefectMapInfo.Columns.Add("LOT_NO", "".GetType)
                m_dtDefectMapInfo.Columns.Add("RACK_NO", 0.GetType)
                m_dtDefectMapInfo.Columns.Add("RACK_ID", "".GetType)
                m_dtDefectMapInfo.Columns.Add("DANSU", 0.GetType)
                m_dtDefectMapInfo.Columns.Add("MAP_X", "".GetType)
                m_dtDefectMapInfo.Columns.Add("MAP_Y", "".GetType)
                m_dtDefectMapInfo.Columns.Add("CAT_CD", "".GetType)
                m_dtDefectMapInfo.Columns.Add("CAT_NAME", "".GetType)
                m_dtDefectMapInfo.Columns.Add("PQC_SUBMIT", True.GetType)
            End If
        End If

        lblTotalDefect.Text = DefectCount.ToString
    End Sub

    Private Sub UpdateRackFrameInfo(ByVal Info As Object, ByVal szItem As String, Optional ByVal iRackNo As Integer = 0)
        If iRackNo > 0 Then
            Dim drRF() As DataRow = m_dtRackFrameInfo.Select("RACK_NO=" & iRackNo.ToString)
            If drRF.Length > 0 Then
                drRF(0)(szItem) = Info
                If szItem.Equals("RACK_ID") Then
                    Dim drDM() As DataRow = m_dtDefectMapInfo.Select("RACK_NO=" & iRackNo.ToString)
                    For Each dr As DataRow In drDM
                        dr(szItem) = Info
                    Next
                End If
            End If
        Else
            If m_dtRackFrameInfo.Rows.Count <= 0 Then
                AddNewRack()
            Else
                For Each dr As DataRow In m_dtRackFrameInfo.Rows
                    dr(szItem) = Info
                Next
            End If
        End If
    End Sub

    Private Sub UpdateDefectMapInfo(ByVal iCol As Integer, ByVal iRow As Integer, Optional ByVal fIsPqc As Boolean = False)
        Dim cell As DataGridViewCell = dgvFrame.Item(iCol, iRow)
        Dim szTmp = cell.Tag.ToString.Replace(vbCr, "").Split(CODE_SPLIT)
        If szTmp.Length >= 2 Then
            Dim szCode As String = szTmp(0)
            Dim szName As String = szTmp(szTmp.Length - 1)

            Dim iFrameIdx As Integer
            If CBool(btnRackTrans.Tag) Then
                iFrameIdx = iFrmQty - CInt(btnFrame.Text) + 1
            Else
                iFrameIdx = CInt(btnFrame.Text)
            End If

            Dim iRowIdx As Integer = iRow
            Dim iColIdx As Integer = iCol
            If CBool(btnFrameTrans.Tag) Then
                iColIdx = CInt(lblFrameRow.Text) - iCol + 1
                iRowIdx = CInt(lblFrameCol.Text) - iRow + 1
            End If

            Dim drFrameDefect() As DataRow = m_dtDefectMapInfo.Select( _
                    String.Format("RACK_NO={0} AND DANSU={1} AND MAP_X='{2}' AND MAP_Y='{3}'", _
                                  lblRackNo.Text, iFrameIdx, iColIdx, iRowIdx))
            If drFrameDefect.Length > 0 Then
                drFrameDefect(0)("CAT_CD") = szCode
                drFrameDefect(0)("CAT_NAME") = szName
                drFrameDefect(0)("RACK_ID") = txtRackID.Text
                drFrameDefect(0)("PQC_SUBMIT") = fIsPqc
            Else
                Dim newRow = m_dtDefectMapInfo.NewRow
                newRow("LOT_NO") = lblLotNo.Text
                newRow("RACK_NO") = lblRackNo.Text
                newRow("RACK_ID") = txtRackID.Text
                newRow("DANSU") = iFrameIdx
                newRow("MAP_X") = iColIdx.ToString
                newRow("MAP_Y") = iRowIdx.ToString
                newRow("CAT_CD") = szCode
                newRow("CAT_NAME") = szName
                newRow("PQC_SUBMIT") = fIsPqc
                m_dtDefectMapInfo.Rows.Add(newRow)
            End If

            Dim drMapInfo() As DataRow = m_dtRackFrameInfo.Select("RACK_NO=" & lblRackNo.Text)
            If drMapInfo.Length <= 0 Then
                AddNewRack()
            End If
        End If

        lblTotalDefect.Text = DefectCount.ToString
        lblDefectInRack.Text = DefectCountInRack.ToString
    End Sub

    Private Sub DeleteDefectInfo(ByVal iCol As Integer, ByVal iRow As Integer)
        Dim iFrameIdx As Integer
        If CBool(btnRackTrans.Tag) Then
            iFrameIdx = iFrmQty - CInt(btnFrame.Text) + 1
        Else
            iFrameIdx = CInt(btnFrame.Text)
        End If

        Dim iRowIdx As Integer = iRow
        Dim iColIdx As Integer = iCol
        If CBool(btnFrameTrans.Tag) Then
            iColIdx = CInt(lblFrameRow.Text) - iCol + 1
            iRowIdx = CInt(lblFrameCol.Text) - iRow + 1
        End If

        Dim drDmDel() As DataRow = m_dtDefectMapInfo.Select( _
            String.Format("RACK_NO={0} AND DANSU={1} AND MAP_X='{2}' AND MAP_Y='{3}'", _
                                  lblRackNo.Text, iFrameIdx, iColIdx, iRowIdx))
        For Each dr As DataRow In drDmDel
            dr.Delete()
        Next

        Dim drFrameDefect() As DataRow = m_dtDefectMapInfo.Select("RACK_NO=" & lblRackNo.Text)
        If drFrameDefect.Length <= 0 Then
            Dim drRfDel() As DataRow = m_dtRackFrameInfo.Select("RACK_NO=" & lblRackNo.Text)
            For Each dr As DataRow In drRfDel
                dr.Delete()
            Next
        End If

        lblTotalDefect.Text = DefectCount.ToString
        lblDefectInRack.Text = DefectCountInRack.ToString
    End Sub

    Private Function DefectCount() As Integer
        Return m_dtDefectMapInfo.Select("", "", DataViewRowState.CurrentRows).Length
    End Function

    Private Function DefectCountInRack() As Integer
        Return m_dtDefectMapInfo.Select("RACK_NO=" & lblRackNo.Text, "", DataViewRowState.CurrentRows).Length
    End Function
#End Region

#Region "テンキー"

    Protected btnKeyAry() As System.Windows.Forms.Button
    Protected lblInput As System.Windows.Forms.Label
    Private fInit As Boolean = False
    Dim InputType As TenkeyType

    Protected Sub InitTenKey()
        PanelTenKey.Visible = False
        PanelTenKey.BringToFront()
        btnKeyAry = New System.Windows.Forms.Button(9) {}
        For i = 0 To 9
            btnKeyAry(i) = CType(PanelTenKey.Controls("btn" & i), Button)
        Next
        btnC = CType(PanelTenKey.Controls("btnC"), Button)
        btnE = CType(PanelTenKey.Controls("btnE"), Button)
        For i = 0 To btnKeyAry.Length - 1
            AddHandler btnKeyAry(i).Click, AddressOf btnKeyAry_Click
        Next i
        AddHandler btnC.Click, AddressOf btnC_Click
        AddHandler btnE.Click, AddressOf btnE_Click
        AddHandler btnBar.Click, AddressOf btnbar_Click
        AddHandler btnL.Click, AddressOf btnL_Click

        AddHandler lblFrameQty.Click, AddressOf TenKeyClick
        AddHandler lblFrameRow.Click, AddressOf TenKeyClick
        AddHandler lblFrameCol.Click, AddressOf TenKeyClick

        AddHandler lblFrameQty.MouseEnter, AddressOf TenKeyMouseEnter
        AddHandler lblFrameRow.MouseEnter, AddressOf TenKeyMouseEnter
        AddHandler lblFrameCol.MouseEnter, AddressOf TenKeyMouseEnter

        AddHandler lblFrameQty.MouseLeave, AddressOf TenkeyMouseLeave
        AddHandler lblFrameRow.MouseLeave, AddressOf TenkeyMouseLeave
        AddHandler lblFrameCol.MouseLeave, AddressOf TenkeyMouseLeave

        lblFrameQty.Tag = TenkeyType.Qty
        lblFrameRow.Tag = TenkeyType.Row
        lblFrameCol.Tag = TenkeyType.Col
    End Sub

#Region "セル動作"
    Protected Sub CallTenKey()
        lblInput.BackColor = Color.Cyan
        Dim iDifTop As Integer = 0
        If lblInput.Parent.Name.ToUpper.StartsWith("TAB") Then
            iDifTop = lblInput.Parent.Parent.Top + lblInput.Parent.Top
        End If
        PanelTenKey.Top = iDifTop + lblInput.Bottom + 5
        PanelTenKey.Left = lblInput.Right - PanelTenKey.Width
        If PanelTenKey.Left < 0 Then
            PanelTenKey.Left = 0
        End If
        If PanelTenKey.Bottom > Me.Height Then
            PanelTenKey.Top = lblInput.Top - PanelTenKey.Height - 5 + iDifTop
        End If
        PanelTenKey.Visible = True

        If Not String.IsNullOrEmpty(lblInput.Tag) AndAlso lblInput.Tag.Equals("BiEq") Then
            btnBar.Visible = True
            btnL.Visible = True
            btnE.Height = 188
            btnC.Left = 193
        Else
            btnBar.Visible = False
            btnL.Visible = False
            btnE.Height = 252
            btnC.Left = 129
        End If
    End Sub

    Protected Sub TenKeyClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        lblInput = CType(sender, Label)
        InputType = lblInput.Tag
        fInit = True
        CallTenKey()
    End Sub

    Protected Sub TenKeyMouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)
        CType(sender, Label).BackColor = COLOR_NAVY
    End Sub

    Protected Sub TenkeyMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
        CType(sender, Label).BackColor = COLOR_YELLOW
        If PanelTenKey.Visible Then
            lblInput.BackColor = COLOR_CYAN
        End If
    End Sub

#End Region

#Region "キー入力処理"
    '******************************************************************************
    '*  キー入力処理
    '******************************************************************************
    Protected Sub btnKeyAry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btnKey As Button = CType(sender, Button)
        Dim strKey As String = btnKey.Text
        If fInit OrElse lblInput.Text.Equals("0") Then
            lblInput.Text = strKey
            fInit = False
        Else
            lblInput.Text &= strKey
        End If
    End Sub

    Private Sub btnDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDot.Click
        Dim chrArr() As Char = lblInput.Text.ToCharArray
        If fInit Then
            lblInput.Text = "0."
            fInit = False
            Exit Sub
        End If
        For Each chara As Char In chrArr
            If chara = CChar(".") Then
                Exit Sub
            End If
        Next
        lblInput.Text &= "."
    End Sub

    Protected Sub btnC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If btnL.Visible Then
            lblInput.Text = String.Empty
        Else
            lblInput.Text = "0"
        End If
        fInit = False
    End Sub

    Protected Sub btnbar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        lblInput.Text &= "-"
    End Sub

    Protected Sub btnL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        lblInput.Text &= "L"
    End Sub

    Protected Sub btnE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        lblInput.BackColor = Color.Yellow
        PanelTenKey.Visible = False
        TenKeyDone()
    End Sub

    Overridable Sub TenKeyDone()
        Select Case InputType
            Case TenkeyType.Qty
                If CInt(lblFrameQty.Text) > 40 Then
                    lblFrameQty.Text = "40"
                End If
                UpdateRackFrameInfo(CInt(lblFrameQty.Text), "FRAME_CNT")
                SetFrameInRack()
            Case TenkeyType.Row
                If CInt(lblFrameRow.Text) > 20 Then
                    lblFrameRow.Text = "20"
                End If
                UpdateRackFrameInfo(CInt(lblFrameRow.Text), "FRM_COL_CNT")
                FrameChanged()
            Case TenkeyType.Col
                If CInt(lblFrameCol.Text) > 26 Then
                    lblFrameCol.Text = "26"
                End If
                UpdateRackFrameInfo(CInt(lblFrameCol.Text), "FRM_ROW_CNT")
                FrameChanged()
        End Select
    End Sub

    Protected Function CheckNum() As Boolean
        If IsNumeric(lblInput.Text) Then
            CheckNum = True
        Else
            CheckNum = False
        End If
    End Function

#End Region

#End Region

End Class